package com.zretc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zretc.dao.VipMapper;
import com.zretc.dao.VipOrdersMapper;
import com.zretc.dao.VipTypeMapper;
import com.zretc.entity.Vip;
import com.zretc.entity.VipOrders;
import com.zretc.entity.VipType;
import com.zretc.service.BackVipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BackVipServiceImpl implements BackVipService {

    @Autowired
    private VipMapper vipMapper;

    @Autowired
    private VipOrdersMapper vipOrdersMapper;

    @Autowired
    private VipTypeMapper vipTypeMapper;


    @Override
    public PageInfo<Vip> getVipInfo(String pageNum,String pageSize) {

        if(pageNum == null){
            pageNum = "1";
        }
        if(pageSize == null){
            pageSize = "10";
        }
        int num = Integer.valueOf(pageNum);
        int size = Integer.valueOf(pageSize);
        //获取分页参数
        PageHelper.startPage(num,size);
        //获取vip信息
        List<Vip> vipInfo = vipMapper.getVipInfo();
        //vip信息分页
        PageInfo<Vip> pageInfo = new PageInfo<>(vipInfo);
        return pageInfo;
    }

    @Override
    public PageInfo<Vip> findByVipId(String vipId,String page,String limit ) {
        String page1 = page;
        if(page1 == null){
            page1="1";
        }
        String limit1 = limit;
        if(limit1 == null){
            limit1 ="10";
        }

        Integer newVipId = Integer.valueOf(vipId);
        Integer pageNum = Integer.valueOf(page1);
        Integer pageSize = Integer.valueOf(limit1);
        PageHelper.startPage(pageNum,pageSize);
        List<Vip> findById = vipMapper.findByVipId(newVipId);
        PageInfo<Vip> vinInfoPage = new PageInfo<>(findById);
        return vinInfoPage;
    }

   /* @Override
    public List<Vip> findByCondition(Map<String, String> param) {

        String vipId = param.get("vipId");
        String accountId = param.get("accountId");
        Integer newAccountId = Integer.parseInt(accountId);
        String vipTypeId = param.get("vipTypeId");
        String vipRegisterTime = param.get("vipRegisterTime");
        String vipStartTime = param.get("vipStartTime");
        String vipEndTime = param.get("vipEndTime");
        Map<String, String> map = new HashMap<>();
        if(vipId == null){
            map.put("vipId",null);
        }else{
            map.put("vipId","%" + vipId + "%");
        }

        if(newAccountId == null){
            map.put("newAccountId",null);
        }else{
            map.put("newAccountId","%" + newAccountId + "%");
        }

        if(vipTypeId == null){
            map.put("vipTypeId",null);
        }else{
            map.put("vipTypeId",vipTypeId);
        } if(vipRegisterTime == null){
            map.put("vipRegisterTime",null);
        }else{
            map.put("vipRegisterTime","%" + vipRegisterTime + "%");
        }

        if(vipStartTime == null){
            map.put("vipStartTime",null);
        }else{
            map.put("vipStartTime","%" + vipStartTime + "%");
        }

        if(vipEndTime == null){
            map.put("vipEndTime",null);
        }else{
            map.put("vipEndTime",vipEndTime);
        }

        return vipMapper.findByCondition(param);
    }*/

    @Override
    @Transactional
    public int addVip(Vip vip) {
        Vip vp = vipMapper.findByAccountId(vip.getAccountId());
        //设置会员编号
        if(vip.getVipId() == null) {
            if (vp.getVipId() == null) {
                // 使用时间戳+随机数自动生成文件名,避免VipId重复问题
                // JDK1.8 日期时间类
                LocalDate now = LocalDate.now();
                DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern("yyyyMMdd");
                // 将当前日期时间转成字符串
                String formatDate = ofPattern.format(now);
                // 随机数
                int random = (int) (Math.random() * 10 + 1);
                // 拼接
                StringBuffer vipId = new StringBuffer();
                vipId.append(formatDate).append(random);
                String vipIdStr = vipId + "1";
                if (vipIdStr != null && !"".equals(vipIdStr)) {
                    Integer newVipId = Integer.valueOf(vipId.toString());
                    vip.setVipId(newVipId);
                }
            }
        }

        //从会员卡自动获取会员类型编号、充值时间、到期时间
        VipOrders vipOrders = vipOrdersMapper.getVipOrdersInfo(vip.getAccountId());
        if(vip.getVipTypeId() == null) {
            vip.setVipTypeId(vipOrders.getVipTypeId());
        }
        if(vip.getVipStartTime() == null) {
            vip.setVipStartTime(vipOrders.getVipStartTime());
        }
        if(vip.getVipEndTime() == null) {
            vip.setVipEndTime(vipOrders.getVipEndTime());
        }


        //设置会员注册时间（第一次充值时间）

        if(vp.getVipRegisterTime() == null){
            if(vip.getVipRegisterTime() == null) {
                vip.setVipRegisterTime(vipOrders.getVipStartTime());
            }
        }


        return vipMapper.insertVipInfo(vip);
    }


    @Override
    public int deleteVipInfoByVipId(String vipId) {

        Integer newVipId = Integer.valueOf(vipId);

        return vipMapper.deleteVipInfoByVipId(newVipId);
    }

    @Override
    @Transactional
    public int updateVipInfoByAccountId(Vip vip) {

            //根据账号查询会员截止日期
            Vip vipo = vipTypeMapper.findEndTimeByaccountId(vip.getAccountId());
            String  deadDate = vipo.getVipEndTime();
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            LocalDateTime ld = LocalDateTime.parse(deadDate,dtf);
            //获取当前日期
            LocalDateTime now = LocalDateTime.now();


            //比较日期设置会员开始时间
            if(ld.isBefore(now) || ld == null){

                vip.setVipStartTime(dtf.format(now));
                vip.setVipEndTime(setVipEndDate(now,vip.getVipTypeId()));
            }
            if(ld.isAfter(now)){
                //根据账号获取账号原到期时间
                Vip vipet = vipTypeMapper.findEndTimeByaccountId(vip.getAccountId());
                    String et = vipet.getVipEndTime();
               //设置新VIP开始时间
                vip.setVipStartTime(et);
                //设置新VIP到期时间
                vip.setVipEndTime(setVipEndDate(LocalDateTime.parse(et,dtf),vip.getVipTypeId()));
            }

            return vipMapper.updateVipInfoByAccountId(vip);

    }
    public String setVipEndDate(LocalDateTime param1,String param2){
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        //判断会员类型,设置不同有效期
        if ("M".equals(param2)) {

            //月卡有效期
            param1 = param1.plusDays(30);


        } else if ("S".equals(param2)) {

            //季卡有效期
            param1 = param1.plusMonths(3);

        } else if ("Y".equals(param2)) {

            //年卡有效期
            param1 = param1.plusYears(1);

        } else {

            //默认设置为月卡
            param1 = param1.plusDays(30);

        }
        return dtf.format(param1);



    }


}
